<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class HomesController extends AppController {

/**
 * This controller does not use a model
 *
 * @var array
 */
	public $uses = array(
			'Category',
			'News',
			'Advertise',
	);

	public function beforeFilter() {
		parent::beforeFilter();
		$this->layout = 'home';
		$title_for_layout = Configure::read('company.name');
		$this->set(compact('page', 'subpage', 'title_for_layout'));
	}
/**
 * Displays a view
 *
 * @param mixed What page to display
 * @return void
 * @throws NotFoundException When the view file could not be found
 *	or MissingViewException in debug mode.
 */
	public function display() {
		//lay danh sach tin tuc theo danh muc hien thi
		$home_category = $this->Category->getNewsDisplayHome();

		//tin HOT
		$sql = "SELECT News.id, News.category_id, News.name, News.new_rewrite,News.new_picture, Category.cat_name, Category.cat_parent_rewrite, Category.cat_rewrite, News.last_update
				FROM news as News, categories_multi as Category
				WHERE News.category_id = Category.id
				AND Category.cat_type = 'news'
				AND News.active = 1 AND Category.cat_active = 1
				AND News.del_flg = 0 AND News.new_hot = 1
				ORDER BY News.last_update DESC
				LIMIT 10";

		$newsHot = $this->News->query($sql);

		//set views
		$this->set(
				array(
						'home_category' => $home_category,
						'newsHot' => $newsHot,
				)
		);

		//render layout
		$path = func_get_args();
		try {
			$this->render(implode('/', $path));
		} catch (MissingViewException $e) {
			if (Configure::read('debug')) {
				throw $e;
			}
			throw new NotFoundException();
		}
	}
}
